var	spaceWidth;
var spaceHeight;
var permSpaceWidth;
var permSpaceHeight;
var oldSpaceWidth;
var oldSPaceHeigth;
var edenSpaceWidth;
var edenHeigthRatio = 0.6;
var edenSpaceHeight;
var suviorSpaceHeight;

var graphWidth;
var graphHeight;
var compileTimeGraphWidth;
var compileTimeGraphHeight;
var classLoaderGraphWidth;
var classLoaderGraphHeight;
var gcTimeGraphWidth;
var gcTimeGraphHeight;
var edenSpaceGraphWidth;
var edenSpaceGraphHeight;
var s0GraphWidth;
var s0GraphHeight;
var s1GraphWidth;
var s1GraphHeight;
var oldGenGraphWidth;
var oldGenGraphHeight;
var permGenGraphWidth;
var permGenGraphHeight;

function initParameters() {
	spaceWidth = Math.round(window.innerWidth * 0.4 / 3 + 0.5);
	spaceHeight = Math.round(window.innerHeight * 0.9 + 0.5);
	permSpaceWidth = spaceWidth * 2 / 3;
	permSpaceHeight = spaceHeight;
	oldSpaceWidth = spaceWidth;
	oldSPaceHeigth = spaceHeight;
	edenSpaceWidth = spaceWidth * 2 / 3;
	//edenHeigthRatio = 0.6;
	edenSpaceHeight = spaceHeight * edenHeigthRatio;
	suviorSpaceHeight = (spaceHeight * (1 - edenHeigthRatio)) / 2 * 0.96;

	graphWidth = Math.round(window.innerWidth * 0.6 + 0.5);
	graphHeight = Math.round(window.innerHeight * 0.9 + 0.5);
	compileTimeGraphWidth = graphWidth;
	compileTimeGraphHeight = graphHeight * 0.1;
	classLoaderGraphWidth = graphWidth;
	classLoaderGraphHeight = graphHeight * 0.1;
	gcTimeGraphWidth = graphWidth;
	gcTimeGraphHeight = graphHeight * 0.1;
	edenSpaceGraphWidth = graphWidth;
	edenSpaceGraphHeight = graphHeight * 0.172;
	s0GraphWidth = graphWidth;
	s0GraphHeight = graphHeight * 0.1;
	s1GraphWidth = graphWidth;
	s1GraphHeight = graphHeight * 0.1;
	oldGenGraphWidth = graphWidth;
	oldGenGraphHeight = graphHeight * 0.172;
	permGenGraphWidth = graphWidth;
	permGenGraphHeight = graphHeight * 0.1;
}


var ctxcomptypes = new Array(
	"source-over","destination-over","source-in","destination-in",
	"source-out","destination-out","source-atop","destination-atop",
	"lighter","xor"
);


var messages = new Array();
messages["permCanvas"] = "Perm Space";
messages["oldCanvas"] = "Old Space";
messages["youthCanvas"] = "Eden Space";
messages["suv1Canvas"] = "S0";
messages["suv2Canvas"] = "S1";
messages["ctCanvas"] = "Compile Time";
messages["clCanvas"] = "Class Loader";
messages["gcCanvas"] = "GC Time";
messages["esCanvas"] = "Eden Space";
messages["s0Canvas"] = "Suvivor 0";
messages["s1Canvas"] = "Suvivor 1";
messages["ogCanvas"] = "Old Space";
messages["pgCanvas"] = "Perm Space";



var fillStyles = new Array();
fillStyles["oldCanvas"] = "rgba(0,185,120,1)";
fillStyles["ogCanvas"] = "rgba(255,185,120,1)";

	
	

/**
 * 
 * 
 * @param canvas
 * @param width
 * @param height
 */
function drawGCSpaceCanvas(canvas, width, height) {
	drawGCSpaceCanvas(canvas,width,heigth,false);
}


function drawGCSpaceCanvas(canvas, width, height, resize, text) {
	if (canvas == null)
		return;
	width = Math.round(width);
	height = Math.round(height);
	if(resize){
		canvas.width = width;
		canvas.height = height;
	}
	var context = canvas.getContext("2d");
	
	context.globalCompositeOperation = ctxcomptypes[1];
		
	context.fillStyle = "rgba(255,255,0,0.5)";
	context.fillRect(0, 0, width, height);
	
	context.globalCompositeOperation = ctxcomptypes[0];
	context.strokeStyle = "rgba(255,0,0,1)";
	context.lineWidth = 1;
	context.strokeRect(0, 0, width, height);
	
	paintLine(context,width,height);
	
	if(text == undefined){
		text = messages[canvas.id];
		if(text == undefined){
			text = canvas.id;
		}
	}
	paintText(canvas,text);
}



function paintText(canvas, text) {
	if(text == undefined || text == null){
		return;
	}
	var context = canvas.getContext("2d");
	context.globalCompositeOperation = ctxcomptypes[0];
	context.fillStyle = "rgb(0,0,0)";
	context.font = "normal 12px Arial";
	context.textAlign = "start";
	context.fillText(text, 0, 12, canvas.width);
}

function drawFilledGCSpaceCanvas(canvas, filledHeight){
	if(canvas == null)
		return;
	var currentHeight = canvas.height;
	var start = currentHeight -filledHeight;
	var context = canvas.getContext("2d");
	//context.globalCompositeOperation = lighter;
	var selectedFillStyle = fillStyles[canvas.id];
	if(selectedFillStyle == undefined)
		selectedFillStyle = "rgba(0,200,100,1)";
	context.fillStyle = selectedFillStyle;
	context.fillRect(0, start, canvas.width, filledHeight);
}

 

function calculateHeapHeight(used,total,spaceHeight){
	return 	spaceHeight * (used / total);
}


function paintLine(context,width,heigth){
	// paint vertical lines
	var lineWidth = 10;
	var lines = width / lineWidth;
	context.strokeStyle="rgba(150,150,150,0.4)";
	context.lineWidth = 0.5;
	for ( var i = 1; i < lines; i++) {
		context.beginPath();
		context.moveTo(0 + i*lineWidth, 0);
		context.lineTo(0 + i*lineWidth, heigth);
		context.stroke();
		context.closePath();
	}
	var lineHeight = 10;
	lines = heigth / lineHeight;
	for ( var i = 1; i < lines; i++) {
		context.beginPath();
		context.moveTo(0, 0 + (i * lineHeight));
		context.lineTo(width, 0 + (i * lineHeight));
		context.stroke();
		context.closePath();
	}
	
}

function clearRect(context,width,height){
	context.clearRect(0, 0, width, height);
}


var histogramWidth = 2;

function drawHistogram(canvas, lines) {
	var context = canvas.getContext("2d");
	var width = canvas.width;
	var height = canvas.height;
	var l = lines.length;
	var selectedFillStyle = fillStyles[canvas.id];
	if(selectedFillStyle == undefined)
		selectedFillStyle = "rgba(0,200,100,1)";
	context.strokeStyle = selectedFillStyle;
	context.lineWidth = histogramWidth;
	var index = 1;
	for ( var i = l-1; i >= 0; i--) {
		var start = width - (context.lineWidth / 2) - context.lineWidth * (index - 1);
		var lineHeight = lines[i];
//		if(start < 0 || height < lineHeight){
//			break;
//		}
		context.beginPath();
		context.moveTo(start,height);
		context.lineTo(start, height- lineHeight);
		context.stroke();
		context.closePath();
		index++;
	}
}
